
package camera;

import static com.googlecode.javacv.cpp.opencv_core.cvScalar;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_MEDIAN;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.VideoInputFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import coordinates.MeanDetector;
import objectExtraction.ColourDetector;
import utilities.AngleFinder;

public class ColorMeanCameraOld implements Runnable {

    final int INTERVAL = 10;// 1sec
    final int CAMERA_NUM; // Default camera for this time
    final int CAMERA_FOV;//angle ofthe camera in degrees
    static CvScalar rgba_min = cvScalar(0, 0, 130, 0);// RED wide dabur birko
    static CvScalar rgba_max = cvScalar(80, 80, 255, 0);

    ColourDetector colDet=new ColourDetector();
    AngleFinder angFind=new AngleFinder();
    MeanDetector meanDet=new MeanDetector();

    IplImage image;
    CanvasFrame canvas = new CanvasFrame("Web Cam Live");
    CanvasFrame path = new CanvasFrame("Threshold");

    private double theta;
    

    public ColorMeanCameraOld() {
        CAMERA_NUM = 0;
        CAMERA_FOV=90;
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        path.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        //path.setContentPane(jp);
    }

    public ColorMeanCameraOld(int camera,int fov) {
        CAMERA_NUM = camera;
        CAMERA_FOV=fov;
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        path.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        // path.setContentPane(jp);
    }

    @Override
    public void run() {
        FrameGrabber grabber = new VideoInputFrameGrabber(CAMERA_NUM);
        int posX;
        try {
            grabber.start();
            Thread.sleep(1000);
            IplImage img;
            IplImage detectThrs;            
            while (true) {
                img = grabber.grab();
                if (img != null) {
                    // show image on window
                    //cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise
                    canvas.showImage(img);
                    //System.out.println("wwwwwwwwwwwwwwwwww");
                    detectThrs=colDet.getThresholdImage1(img);
                    //System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
                    cvSmooth(detectThrs, detectThrs, CV_MEDIAN, 15);
                    //cvSaveImage(++ii + "GreenThreshold.jpg", imgThreshold);
                    //System.out.println("hghsgsgsg");
                    //detectThrs = this.getThresholdImage1(img);
                    posX=meanDet.getMeanX(detectThrs);
                    theta=angFind.angleFinder(posX,detectThrs.width(), CAMERA_FOV);
                    path.showImage(detectThrs);
                    //System.out.println("theeeee");

                }
                //Thread.sleep(INTERVAL);
            }
        } catch (Exception e) {
            System.out.println("Exception in Color Mean Detector");
            e.printStackTrace();
        }
    }

    /**
     * @return the theta
     */
    public double getTheta() {
        return theta;
    }

   

}
